// pages/carts/carts.js
import myrequest from "../../utils/myrequest.js"
Page({
    
    /**
     * 页面的初始数据
     */
    data: {
        carts:[],
        show:false,
        num:null,
        count:1,
        result:['a','b'],
        checked:false,
        command:[],
        total:0,
        edit:"编辑",
        btnzi:"结算",
        newarrs:null,
        let:true
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    // methods
    clickfn(){
        this.setData({
            let:!this.data.let
        })
    },
    editfn(){
        if(this.data.edit=="编辑"){
            this.setData({
                edit:"完成",
                btnzi:"删除"

            })
        }else{
            this.setData({
                edit:"编辑",
                btnzi:"结算"
            })

        }
        
    },
    totalfn(){
        wx.navigateTo({
            url: '/pages/order/order?orders='+JSON.stringify(this.data.newarrs)
          })
    },


    groupfn(event){
        
        console.log(event)
        let index = event.currentTarget.dataset.id
        let value = event.detail.value
        if(value.length==1){
        this.data.carts[index].checked = true
        }else{
        this.data.carts[index].checked = false
        }
        console.log(this.data.carts)
        let newarrs = this.data.carts.filter((item,index)=>{
        return item.checked == true
        })
        
        
        

        let total = newarrs.reduce((num,item)=>{
        console.log(item)
        return num + item.price*item.count
        },0)
        console.log(total)
        this.setData({
            total,
            newarrs,
        })

    //     
    //     let len = event.detail.value.length
    //    console.log(len)
       
    },

    checkfn(event){
        this.setData({
            checked:!this.data.checked
        })
        this.data.carts.forEach((item)=>{
            item.checked = true
        })
        console.log(this.data.carts)
    },

    // onChange1(event) {
    //     console.log(event)
    //     this.setData({
    //       result: event.detail,
    //       checked:!this.data.checked
    //     });
    // },

    onChange(event){
        let count = event.detail
        this.setData({
            count
        })
    },

    async getcommand(){
        let {data} = await myrequest({url:"/cartsStorges"})
        this.setData({
            command:data.data
        })
    },

     getcarts(){
         
        let carts = wx.getStorageSync('carts')
        carts.forEach((item)=>{
            item.count = this.data.count
            item.checked = false
        })
        console.log(carts)
        this.setData({
            carts
        })
        // this.data.carts.forEach((item)=>{
        //     item.checked = false
        // })
        console.log(this.data.carts)
    },
    // 有几件商品
    num(){
        let num = this.data.carts.length
        this.setData({
            num
        })
      
    },

    onClose() {
        this.setData({ show: false });
      },




    onLoad: function (options) {
       
        this.num()
        this.getcommand()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getcarts()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})